<?php

//会员激活
class MembersActivationModel extends CActiveRecord {

    /**
     * @return string the associated database table name
     */
    public function tableName() {
        return '{{members_activation}}';
    }

    /**
     * @return array validation rules for model attributes.
     */
    public function rules() {
        // NOTE: you should only define rules for those attributes that
        // will receive user inputs.
        return array(
            array('email', 'required'),
            array('status', 'numerical', 'integerOnly' => true),
            array('email', 'length', 'max' => 255),
            array('code', 'length', 'max' => 32),
            array('endtime', 'length', 'max' => 11),
            // The following rule is used by search().
            // @todo Please remove those attributes that should not be searched.
            array('id, email, code, endtime, status', 'safe', 'on' => 'search'),
        );
    }

    /**
     * @return array relational rules.
     */
    public function relations() {
        // NOTE: you may need to adjust the relation name and the related
        // class name for the relations automatically generated below.
        return array(
        );
    }

    /**
     * @return array customized attribute labels (name=>label)
     */
    public function attributeLabels() {
        return array(
            'id' => 'ID',
            'email' => 'Email',
            'code' => '激活码',
            'endtime' => '过期时间',
            'status' => '是否可用',
        );
    }

    /**
     * Returns the static model of the specified AR class.
     * Please note that you should have this exact method in all your CActiveRecord descendants!
     * @param string $className active record class name.
     * @return MembersActivationModel the static model class
     */
    public static function model($className = __CLASS__) {
        return parent::model($className);
    }

    //根据邮箱获取最新激活码
    public function getModelByEmail($email) {
        $model = $this->find('email = ? order by endtime desc', array($email));
        return $model;
    }

    public function JudgeSent($email) {
        $model = $this->getModelByEmail($email);
        if (!$model) {
            return 0;
        }
        $time = $_SERVER['REQUEST_TIME'] + 48 * 3600 - $model->endtime;
        if ($time < 120) {
            return $time;
        }
        return 0;
    }

    public function JudgeFindPasswordSent($email) {
        $model = $this->getModelByEmail($email);
        if (!$model) {
            return 0;
        }
        $time = $_SERVER['REQUEST_TIME'] + 72 * 3600 - $model->endtime;
        if ($time < 120) {
            return $time;
        }
        return 0;
    }

    //激活验证
    public function judgeVerify($code) {
        $model = $this->find('code = ? order by endtime desc', array($code));
        if (!$model) {
            return $return = array(
                'status' => -1,
                'message' => '无效的激活码!',
            );
        }
        if ($model->status == 1) {
            return array(
                'status' => 1,
                'message' => "您的邮箱{$model->email}激活码已经失效！",
                'email' => $model->email,
            );
        }
        $time = $_SERVER['REQUEST_TIME'] - $model->endtime;
        if ($time > 0) {
            return array(
                'status' => 0,
                'message' => "您的邮箱{$model->email}激活码已经失效！",
                'email' => $model->email,
            );
        }
        $model->status = 1;
        if ($model->update()) {
            $menber = MembersModel::model()->getMemberByEmail($model->email);
            //激活
            UserRegister::getInstance()->userActivate($menber->uid);
            //删除使用过的
            $model->delete();
            //邀请注册
            MembersInviteModel::model()->inviteActivate($menber);
            return array(
                'status' => 1,
                'message' => "激活成功！",
                'email' => $model->email,
            );
        }
        return array(
            'status' => 0,
            'message' => "激活失败！！",
            'email' => $model->email,
        );
    }

    public function judgeFindPwdVerify($code) {
        $model = $this->find('code = ? order by endtime desc', array($code));
        if (!$model) {
            return $return = array(
                'status' => -1,
                'message' => '您的邮箱{$model->email}激活码已经失效！',
            );
        }

        $time = $_SERVER['REQUEST_TIME'] - $model->endtime;
        if ($time > 0) {
            return array(
                'status' => 0,
                'message' => "您的邮箱{$model->email}激活码已经失效！",
                'email' => $model->email,
            );
        }

        $model->status = 1;
        if ($model->update()) {
            return array(
                'status' => 1,
                'message' => "激活成功！",
                'email' => $model->email,
            );
        }
        return array(
            'status' => 0,
            'message' => "您的邮箱{$model->email}激活码已经失效！",
            'email' => $model->email,
        );
    }

}
